"use strict";
const session = require('./session.js')
let request={
  post(url,data,type){
    return this.request(url,data,type,"POST")
  },
  get(url,data,type){
    return this.request(url, data, type, "GET")
  },
  request(url, data, dataType,method){
    var contentType;
    if (dataType === 'json') {
      contentType = 'application/json';
    }
    else {
      contentType = 'application/x-www-form-urlencoded';
    }
    data.session_key = session.getSession()
    console.log("httprequest-->requestBody=",data)
    return new Promise((resolve,reject)=>{
      wx.request({
        url: url,
        data: data,
        header: {
          'content-type': contentType
        },
        method: method,
        dataType: 'json',
        responseType: 'text',
        success: function(res) {
          const data = res.data
          if(data.result=="success"){
            resolve(data)
          }else{
            if (data.msg =="session is fail"){
              session.deleteSession()
              wx.navigateTo({
                url: '/pages/login/login',
                success: function(res) {},
              })
              console.error(data)
            }else{
              reject(data)
            }
          }
        },
        fail: function(res) {
          reject(res)
        },
        complete: function(res) {},
      })
    })
  }
}
module.exports=request